import sensor from '@system.sensor';
import prompt from '@system.prompt';
import {HuaweiIdAuthParamsHelper, HuaweiIdAuthManager} from  '@hmscore/hms-jsb-account';
import {HMSHealthKitScope} from '@hw-hmscore/hms-js-health'

export default {
    data: {
        signIn:false,
        list:[
            {
                title:'心率',
                field:'subscribeHeartRate',
                value:'获取失败',
                color:'faa'
            },
            {
                title:'气压',
                field:'subscribeBarometer',
                value:'获取失败',
                color:'aaf'
            },
            {
                title:'计步',
                field:'subscribeStepCounter',
                value:'获取失败',
                color:'afa'
            },
        ]
    },
    onInit() {
        for (const item of this.list) {
            this[item.field](item);
        }
        // 步骤1：声明需要申请的Scope权限
        let scopeList = [
        // 查看HUAWEI Health Kit中的步数
            {
                mScopeUri: HMSHealthKitScope.HEALTHKIT_STEP_READ,
            },
            // 查看HUAWEI Health Kit中的心率数据
            {
                mScopeUri: HMSHealthKitScope.HEALTHKIT_HEARTRATE_READ,
            }];
        // 步骤2：构造登录参数
        var signInOption = new HuaweiIdAuthParamsHelper().setId().setProfile().setScopeList(scopeList).build();
        // 步骤3：拉起授权页面及处理登录结果
        // HuaweiIdAuthManager.getAuthApi方法返回huaweiIdAuth对象，调用huaweiIdAuth.getSignInIntent方法，拉起登录授权界面
        HuaweiIdAuthManager.getAuthApi().getSignInIntent(signInOption).then((result) => {
            // 登录成功，获取用户的华为帐号信息
            console.info("signIn success");
            prompt.showToast({message:'signIn success'})
            console.info(JSON.stringify(result));

        }).catch((error) => {
            // 登录失败
            prompt.showToast({message:"signIn fail" + JSON.stringify(error)})
            console.error("signIn fail");
            console.error(JSON.stringify(error));
        });
    },
    onDestroy(){
        for (const item of this.list) {
            sensor['un' + item.field]();
        }
    },
    subscribeHeartRate(item){
        sensor.subscribeHeartRate({
            success: function(ret) {
                console.log('心率:' + ret.heartRate);
                item.value = ret.heartRate
            },
            fail: function(data, code) {
                console.log('Subscription failed. Code: ' + code + '; Data: ' + data);
            },
        });
    },
    subscribeBarometer(item){
        sensor.subscribeBarometer({
            success: function(ret) {
                console.log('气压:' + ret.pressure);
                item.value = ret.pressure.toFixed(2)
            },
            fail: function(data, code) {
                console.log('Subscription failed. Code: ' + code + '; Data: ' + data);
            },
        });
    },
    subscribeStepCounter(item){
        sensor.subscribeStepCounter({
            success: function(ret) {
                console.log('计步:' + ret.steps);
                item.value = ret.steps
            },
            fail: function(data, code) {
                console.log('Subscription failed. Code: ' + code + '; Data: ' + data);
            },
        });
    }
}
